package framework;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Vector;
import com.Const;
import com.PublicSystem;
/**
 * 生成主树形栏
 * @author host
 *
 */
public class Tree {
	PublicSystem sys = PublicSystem.getInstance();
	StringBuffer sb = new StringBuffer();
	Meun m = new Meun();
	/**
	 * 创建树形栏
	 * @throws Exception
	 */
	public void createTree() throws Exception {
		String pid = "";
		String name = "";
		String sid = "";
		String sname = "";
		String url = "";
		sb.append("[");
		Vector res = m.menuPid();
		for(int i = 0; i < res.size(); i++) {
			pid = (String) ((Vector) res.elementAt(i)).elementAt(0);
			name = (String) ((Vector) res.elementAt(i)).elementAt(2);
			sb.append("{id: \"" + pid + "\", text: \"" + name + "\"},");	
			Vector res1 = m.menuSid(pid);
			if(null != res1) {			
				createNodeTree(pid);
			}
		}
		sb.append("]");
		sb.replace(sb.length() - 2, sb.length() - 1, "");
		String outputPath = Const.server_path + "/page/company/system/tree.txt";
		OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8");
		BufferedWriter bw = new BufferedWriter(write);
		PrintWriter pw = new PrintWriter(bw);
		pw.println(sb);
		pw.flush();
		pw.close();	
	}
	/**
	 * 创建节点
	 * @param pid
	 * @throws Exception
	 */
	public void createNodeTree(String pid) throws Exception {			
		String checked = "false";
		String sid = "";
		String sname = "";
		String url = "";
		Vector res = m.menuSid(pid);
		if(null != res) {			
			for(int i = 0; i < res.size(); i++) {				
				sid = (String) ((Vector) res.elementAt(i)).elementAt(1);
				sname = (String) ((Vector) res.elementAt(i)).elementAt(2);
				url = (String) ((Vector) res.elementAt(i)).elementAt(3);
				if("#".equals(url)) {					
					sb.append("{id: \"" + sid + "\", text: \"" + sname + "\",pid: \"" + pid + "\",checked: \"" + checked + "\"},");
					createNodeTree(sid);
				} else {					
					sb.append("{id: \"" + sid + "\", text: \"" + sname + "\",pid: \"" + pid + "\",checked: \"" + checked + "\"},");
				}					
			}			
		}			
	}		
}